package cn.lintcode;

import java.lang.reflect.Array;
import java.util.*;

/**
 * @author labvi
 * @version 1.0.0
 * <p>
 * 二叉树层次遍历
 */
public class BinaryTree {
    public static class TreeNode {
        public int val;
        public TreeNode left, right;

        public TreeNode(int val) {
            this.val = val;
            this.left = this.right = null;
        }
    }

    public List<List<Integer>> levelOrder(TreeNode root) {
        // write your code here
        if (null == root) {
            return new ArrayList();
        }
        LinkedList<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> result = new ArrayList<>();
        int currentLevelNum = 1;
        queue.addFirst(root);
        while (!queue.isEmpty()){
            List<Integer> a = new ArrayList<>();
            int temp = 0;
            for (int i = 0; i < currentLevelNum; i++) {
                TreeNode first = queue.pollFirst();
                if (first.left != null) {
                    queue.addLast(first.left);
                    temp++;
                }
                if (first.right != null) {
                    queue.addLast(first.right);
                    temp++;
                }
                a.add(first.val);
            }
            result.add(a);
            currentLevelNum = temp;
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(5>>1);
    }
}
